/*
clear 
use "/data/rawdata/cleaned/fek_fe_cost_2005.dta",clear
forvalues y = 2006(1)2018 {
	append using "/data/rawdata/cleaned/fek_fe_cost_`y'.dta"
}

foreach var of varlist pensionsutbetalningar utbildning sjukochhalsovard andrapersonalkostnader loneskatter lagstadgadesocialaavgifter ovrigaavgifter pensionsavsattningar ovrigapensionskostnader avgangsvederlag erhallnabidragochersattningarfor lonerochandraersattningar {
	drop if !mi(`var') & `var'>0
	replace `var' = 0 if mi(`var')
	replace `var' = - `var'/1000
}
gcollapse (sum) pensionsutbetalningar utbildning sjukochhalsovard andrapersonalkostnader loneskatter lagstadgadesocialaavgifter ovrigaavgifter pensionsavsattningar ovrigapensionskostnader avgangsvederlag erhallnabidragochersattningarfor lonerochandraersattningar, by(lopnr_peorgnr year)

order lopnr_peorgnr year avgangsvederlag loneroch
save "$datapath/firmsample.dta", replace
keep lopnr_peorgnr 
duplicates drop 
save "$datapath/firmsample_firms", replace

foreach num of numlist 2004(1)2018 {
	di "Year: `num'"
	qui {
	clear
	use lopnr_peorgnr lopnr year manfran mantill lonfink yrkstallnku using "/data/rawdata/cleaned/jobb_`num'"
	keep if yrkstallnku=="2"
	drop yrkstallnku
	merge m:1 lopnr_peorgnr using "$datapath/firmsample_firms", keep(3) nogen 
	gcollapse (sum) lonfink (min) manfran (max) mantill, by(lopnr lopnr_peorgnr)
	gen imputed = cond(manfran==0,1,cond(mantill==0,12,0))
	replace manfran = 1  if manfran==0
	replace mantill = 12 if mantill ==0
	gen year = `num'

	if `num'>2004 {
		append using "$datapath/firmsample_jobb"
	}
	save "$datapath/firmsample_jobb", replace
	}
}

clear
use 		"$rawdatapath/DS_Lev_AF_hist_varsel.dta"
rename peorglopnr lopnr_peorgnr 
rename cfarlopnr lopnr_cfarnr 
merge 1:m lopnr_varsellnr lopnr_hist_varsellnr using "$rawdatapath/DS_Lev_AF_hist_varselperson.dta", keep(3) nogen
merge m:1 lopnr_varsellnr using  "$datapath/A1_varselorsak.dta", keep(3) nogen 
gen year = substr(anstupphor_dat,1,4)
destring year, replace
keep if inrange(year,2005,2015)
keep lopnr_peorgnr lopnr year anstupphor_dat varselkat antvar* 
bys lopnr_peorgnr lopnr (year): keep if _n==_N
keep lopnr_peorgnr lopnr year varselkat  antvar* 
drop if mi(lopnr_peorgnr) | mi(lopnr)
isid lopnr_peorgnr lopnr
save "$datapath/notified", replace
*/

clear
use "$datapath/firmsample_jobb"
merge m:1 year using /data/rawdata/cleaned/cpi_sweden, keep(3) nogen 
gen monthsworked  = mantill - manfran + 1
replace lonfink = lonfink / cpi 
gen earnmon_cpi   = lonfink/(monthsworked)
bys lopnr_peorgnr lopnr (year): gen year_l1=year[_n-1] 
by  lopnr_peorgnr lopnr (year): gen year_f1=year[_n+1]
by  lopnr_peorgnr lopnr (year): gen year_f2=year[_n+2]
by  lopnr_peorgnr lopnr (year): gen earnmon_cpi_l1=earnmon_cpi[_n-1] 

gen temp = !mi(year_f1) 
egen firmexists = max(temp), by(lopnr_peorgnr year)
drop temp
gen sep = cond(!inlist(year,2018), cond(firmexists==1, ///
cond(year_f1==.,1,0),0),0)
merge m:1 lopnr_peorgnr lopnr using  "$datapath/notified"
drop if _merge==2
gen displaced = _merge==3
drop _merge 

gen sp     = cond(sep==1,                  max(lonfink - earnmon_cpi_l1*(mantill-manfran+1),0),0)
gen sp_not = cond(sep==1,cond(displaced==1,max(lonfink - earnmon_cpi_l1*(mantill-manfran+1),0),0),0)

/*
gen sp     = cond(sep==1,lonfink - earnmon_cpi_l1*(mantill-manfran+1),0)
gen sp_not = cond(sep==1,cond(displaced==1,lonfink - earnmon_cpi_l1*(mantill-manfran+1),0),0)
*/
gen sep_d = cond(sep==1,cond(displaced==1,1,0),0)

gcollapse (sum) sep_d sep sp sp_not lonfink, by(lopnr_peorgnr year)

foreach var of varlist sp sp_not lonfink {
	replace `var'=`var'/1000
}

merge 1:1 lopnr_peorgnr year using "$datapath/firmsample.dta"
keep if _merge==3 
drop _merge 

save "$datapath/firsample_aggr", replace




